package com.risk.ctrl.utils;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class Md5Utils {
    /**
     * md5加密为16进制
     *
     * @param text
     * @return
     */
    public static String md52Hex(String text) {
        StringBuilder hexString = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(text.getBytes());
            byte messageDigest[] = digest.digest();

            // Create Hex String
            hexString = new StringBuilder();
            for (byte aMessageDigest : messageDigest) {
                String h = Integer.toHexString(0xFF & aMessageDigest);
                if (h.length() < 2) {
                    h = "0" + h;
                }
                hexString.append(h);
            }
        } catch (NoSuchAlgorithmException e) {
            log.error("md5加密失败params={}", text, e);
            return null;
        }
        return hexString.toString().toLowerCase();
    }

    public static void main(String[] args) {
        String str = "123";
        String md5 = md52Hex(str);
        log.info("md5={}", md5.toUpperCase());
    }
}
